package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.UsualQuestion;
import ywpt.web.common.PublicFunction;

public class UsualQuestionServiceImpl extends HibernateEntityDao<UsualQuestion> implements UsualQuestionService{
	public UsualQuestionServiceImpl(){
		
	}

	@Override
	public UsualQuestion getUsualQuestionById(Long id) {
		return this.get(id);
	}

	@Override
	public String createUsualQuestionCode() {
		DetachedCriteria query = DetachedCriteria.forClass(UsualQuestion.class);
		String questionCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<UsualQuestion> usualQuestions = this.getUsualQuestions(query);
		if ( usualQuestions.iterator().hasNext() )	
		{
			code = usualQuestions.iterator().next().getCode();		
			maxId = new Integer(code);
		}
		questionCode=PublicFunction.getNextCode("", maxId, false,4); 
		return questionCode;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsualQuestion> getQuestionByQuestionTitle(String title,
			int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(UsualQuestion.class);
		if ( title != null && !title.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("title", "%" + title + "%"),  
							          Restrictions.like("code", "%" + title + "%")));
		}
		query.addOrder(Order.asc("code"));
		List<UsualQuestion> usualQuestionList = new ArrayList<UsualQuestion>();
		List<UsualQuestion> usualQuestionLists = new ArrayList<UsualQuestion>();
		UsualQuestion usualQuestion =new UsualQuestion();
		
		usualQuestionList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<UsualQuestion> itr = usualQuestionList.iterator(); itr.hasNext(); i++ ) 
		{
			usualQuestion = itr.next();
			String ss = PublicFunction.format(usualQuestion.getCode() + "",5, "  ", 1)
			          + usualQuestion.getTitle(); 
			
			usualQuestion.setTitle(ss);
			usualQuestionLists.add(usualQuestion);
		}
		return usualQuestionLists;
	}

	@Override
	public int getQuestionCountByTitle(String title) {
		DetachedCriteria query = DetachedCriteria.forClass(UsualQuestion.class);
		if ( title != null && !title.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("title", "%" + title + "%" ),  
							          Restrictions.like("code", "%" + title + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public int getUsualQuestionCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsualQuestion> getUsualQuestions(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<UsualQuestion> getUsualQuestions() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsualQuestion> getUsualQuestions(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void removeUsualQuestion(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveUsualQuestion(UsualQuestion usualQuestion) {
		super.save(usualQuestion);
	}

	@Override
	public void updatUsualQuestion(UsualQuestion usualQuestion) {
		super.update(usualQuestion);
	}

	@Override
	public String setUsualDetails(Long id) {
		return this.get(id).getDetail();
	}
}
